# Created with pycharm
# author:ju bai
# Date:2021/7/20
# Time:20:47
import HTMLTestRunner
import sys
import time
import unittest
import os
from test0720 import test1


def createsuite():
    # addTest
    # suite = unittest.TestSuite()
    # suite.addTest(test1.Baidu1("test_hao"))
    # suite.addTest(test1.Baidu1("test_baidu"))
    # return suite

    # makeSuit
    # 一次性导入所有脚本（test_）
    # suite = unittest.TestSuite()
    # suite.addTest(unittest.makeSuite(test1.Baidu1))
    # return suite

    # TestLoader
    # suite1 = unittest.TestLoader.loadTestsFromTestCase(test1.Baidu1)
    # suite2 = unittest.TestLoader.loadTestsFromTestCase(test1.Baidu1)
    # suite = unittest.TestLoader([suite1,suite2])
    # return suite

    # discover
    # 执行某个文件夹下的所有某种类型或某种格式命名的所有文件的脚本
    discover = unittest.defaultTestLoader.discover("../test0720", pattern="test*.py", top_level_dir=None)
    print(discover)
    return discover


if __name__ == "__main__":
    # suite = createsuite()
    # # verbosity=1 每运行成功一个会有一个.，运行错误F
    # # verbosity=2 运行细节标注
    # # 所以参数代表输出结果的详细程度
    # runner = unittest.TextTestRunner(verbosity=2)
    # runner.run(suite)

    # HTML报告生成
    # 1. 创建一个文件夹
    # sys.path[0]：本地路径到/test0720
    curpath = sys.path[0]
    # sys.path 是一个路径集合数组
    print(sys.path)
    print(sys.path[0])
    # 当当前路径下resultreport文件夹不存在时，就创建一个
    if not os.path.exists(curpath+'/resultreport'):
        os.makedirs(curpath+'/resultreport')
    # 2. 解决重复命名问题
    now = time.strftime("%Y-%m-%d-%H %M %S", time.localtime(time.time()))
    # time.time()：时间戳
    print(time.time())
    # time.localtime()：本地时间
    print(time.localtime(time.time()))
    # 文件名是路径加上文件的名称
    filename = curpath+'/resultreport/'+ now + 'resultreport.html'
    # 打开HTML文件，写入测试套件运行结果
    # fp 输入流
    with open(filename, 'wb') as fp:
        # 括号内参数是HTML报告里的参数
        runner = HTMLTestRunner.HTMLTestRunner(
            stream=fp,
            title=u"测试报告",
            description=u"用例执行情况",
            verbosity=2
        )
        suite = createsuite()
        runner.run(suite)